package vn.com.vega.music.view.custom;

import vn.com.vega.chacha.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ChaChaDialog extends Dialog {
	private TextView mTitle;
	private TextView mMessage;
	private ImageButton mCloseBtn;
	private LinearLayout mBottomLayout;
	private Context mContext;
	private LinearLayout mContentContainer;
	private OnCloseDialogListener onCloseDialogListener;
	private View mContentView;
	private View mHeaderView;

	public ChaChaDialog(Context context,
			OnCloseDialogListener closeDialogListener) {
		super(context, R.style.Theme_CustomDialog);
		// TODO Auto-generated constructor stub
		mContext = context;
		onCloseDialogListener = closeDialogListener;
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		/* this.getWindow(). */setContentView(R.layout.layout_dialog);
		mTitle = (TextView) findViewById(R.id.tv_title_dialog);
		mMessage = (TextView) findViewById(R.id.tv_dialog_message);
		mCloseBtn = (ImageButton) findViewById(R.id.bt_close_dialog);
		mCloseBtn.setOnClickListener(closeClickListener);
		mBottomLayout = (LinearLayout) findViewById(R.id.layout_bottom_dialog);
		mContentContainer = (LinearLayout) findViewById(R.id.layout_dialog_content);
		mHeaderView = findViewById(R.id.layout_header);
		setCancelable(false);
		setCanceledOnTouchOutside(true);
		mCloseBtn.setTag(this);
	}

	View.OnClickListener closeClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dismiss();
		}
	};

	@Override
	public void dismiss() {
		super.dismiss();
		if (onCloseDialogListener != null) {
			onCloseDialogListener.onClose();
		}
	}

	public void hideHeader() {
		mHeaderView.setVisibility(View.GONE);
	}

	/**
	 * Set title for dialog
	 * 
	 * @param title
	 *            title of dialog
	 */
	@Override
	public void setTitle(CharSequence title) {
		mTitle.setText(title);
	}

	/**
	 * set message for dialog
	 * 
	 * @param msg
	 *            message of dialog
	 */
	public void setMessage(String msg) {
		mMessage.setText(msg);
	}

	/**
	 * Add button with action when button is clicked.
	 * 
	 * @param btnLable
	 *            Label of button
	 * @param btClickListener
	 *            Action call when button is clicked
	 */
	public void addButton(String btnLable, View.OnClickListener btClickListener) {
		Button bt = initBtn();
		bt.setText(btnLable);
		// bt.setTextSize(mContext.getResources().getDimension(R.dimen._16sp));
		bt.setOnClickListener(btClickListener);
		mBottomLayout.addView(bt);
	}

	private Button initBtn() {
		Button result = new Button(mContext);
		result.setTextColor(Color.WHITE);
		result.setBackgroundResource(R.drawable.bg_button);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				(int) mContext.getResources().getDimension(R.dimen.zero),
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.leftMargin = (int) mContext.getResources().getDimension(
				R.dimen.margin_button_diaglog);
		lp.rightMargin = (int) mContext.getResources().getDimension(
				R.dimen.margin_button_diaglog);
		lp.weight = 1;
		result.setLayoutParams(lp);
		return result;
	}

	/**
	 * Add action when click close button default (on top - right)
	 * 
	 * @param clickListener
	 *            action call when close is clicked
	 */
	public void setCloseClickListener(View.OnClickListener clickListener) {
		dismiss();
		mCloseBtn.setOnClickListener(clickListener);
	}

	/**
	 * Add close button, when click close dialog
	 * 
	 * @param closeLable
	 *            label of close button
	 */
	public void addDefaultOkBtn(String closeLabel) {
		View.OnClickListener okClickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ChaChaDialog.this.dismiss();
				if (onCloseDialogListener != null) {
					onCloseDialogListener.onClose();
				}
			}
		};
		addButton(closeLabel, okClickListener);
		hideCloseDefaultBtn();
	}

	/**
	 * When no need any button, hide bottom view
	 */
	public void hideBottomView() {
		mBottomLayout.setVisibility(View.GONE);
	}

	@Override
	public void setContentView(View content) {
		mMessage.setVisibility(View.GONE);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		mContentContainer.addView(content, lp);
		mContentView = content;
	}

	public View getContentView() {
		if (mContentView != null) {
			return mContentView;
		} else {
			return mMessage;
		}
	}

	public void hideCloseDefaultBtn() {
		mCloseBtn.setVisibility(View.INVISIBLE);
	}

	public interface OnCloseDialogListener {
		public void onClose();
	}
}
